package com.jcl.recruit.console.logic;

import com.jcl.recruit.console.model.ConsoleVerificationCode;
import com.jcl.recruit.core.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName VerificationCodeLogic

 * @Author yyliu
 * @Date 2018/9/18 9:37
 * @Version 1.0
 **/
@Component
public class VerificationValidateLogic {

    @Autowired
    private VerificationCodeLogic verificationCodeLogic;


    public int validatePhoneRegisterCode(String phone, String code) {
        ConsoleVerificationCode verificationCode = verificationCodeLogic.validateCode(phone,
                Constants.VerificationInfo.VERIFY_TYPE_PHONE_REGISTER, code);

        return validateCode(verificationCode);
    }


    public int validateEmailRegisterCode(String email, String code) {
        ConsoleVerificationCode verificationCode = verificationCodeLogic.validateCode(email,
                Constants.VerificationInfo.VERIFY_TYPE_EMAIL_REGISTER, code);

        return validateCode(verificationCode);
    }

    public int validateCode(ConsoleVerificationCode verificationCode) {
        if (null == verificationCode) {
            // 验证码不正确
            return 1;
        } else {
            Date abateDate = verificationCode.getAbateDate();
            if (abateDate.before(new Date())) {
                // 验证码失效
                return 2;
            }
        }
        return 0;
    }
}
